package co.th.bjc.md.dbonline.control;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import co.th.bjc.md.dbonline.bean.HospitalDepartmentDBOnlineBean;
import co.th.bjc.md.helper.HospitalDepartmentHelper;
import co.th.bjc.md.model.HospitalDepartment;
import co.th.bjc.md.model.ImpRating;
import co.th.bjc.md.model.Person;
import co.th.bjc.md.model.RpRating;

public class HospitalDepartmentDBOnlineControl {

	private static Logger _log = Logger.getLogger(HospitalDepartmentDBOnlineControl.class);
	
	private HospitalDepartmentDBOnlineBean setHospitalDepartmentToHospitalDepartmentDBOnlineBean(HospitalDepartment model){
		HospitalDepartmentDBOnlineBean bean = new HospitalDepartmentDBOnlineBean();
		bean.setHospitalCode(model.getHospitalCode().intValue());
		bean.setHospitalDepartmentCode(model.getHospitalDepartmentCode().intValue());
		bean.setHospitalDepartmentNameTH(model.getHospitalDepartmentNameTh());
		bean.setHospitalDepartmentNameEN(model.getHospitalDepartmentNameEn());
		bean.setBedCapMin(model.getBedCapacityMin().intValue());
		bean.setBedCapMax(model.getBedCapacityMax().intValue());
		if (model.getDirector() != null){
			bean.setDirectorCode(model.getDirector().getPersonCode());
			bean.setDirectorTitle(model.getDirector().getPersonTitle());
			bean.setDirectorFirstName(model.getDirector().getPersonFirstName());
			bean.setDirectorLastName(model.getDirector().getPersonLastName());
			bean.setDirectorTelephone(model.getDirector().getTelephone());
			bean.setDirectorEmail(model.getDirector().getEmail());
			bean.setDirectorIMP(model.getDirector().getImp().getImpCode());
			bean.setDirectorRP(model.getDirector().getRp().getRpCode());
			bean.setDirectorRemark(model.getDirector().getRemark());
		}
		return bean;
	}
	
	private HospitalDepartment setHospitalDepartmentDBOnlineBeanToHospitalDepartment(HospitalDepartmentDBOnlineBean bean){
		HospitalDepartment model = new HospitalDepartment();
		model.setHospitalCode(BigDecimal.valueOf(bean.getHospitalCode()));
		model.setHospitalDepartmentCode(BigDecimal.valueOf(bean.getHospitalDepartmentCode()));
		model.setHospitalDepartmentNameTh(bean.getHospitalDepartmentNameTH());
		model.setHospitalDepartmentNameEn(bean.getHospitalDepartmentNameEN());
		model.setBedCapacityMin(BigDecimal.valueOf(bean.getBedCapMin()));
		model.setBedCapacityMax(BigDecimal.valueOf(bean.getBedCapMax()));
		if (model.getDirector() != null){
			Person director = new Person();
			director.setPersonCode(bean.getDirectorCode());
			director.setPersonTitle(bean.getDirectorTitle());
			director.setPersonFirstName(bean.getDirectorFirstName());
			director.setPersonLastName(bean.getDirectorLastName());
			director.setTelephone(bean.getDirectorTelephone());
			director.setEmail(bean.getDirectorEmail());
			director.setImp(new ImpRating(bean.getDirectorIMP()));
			director.setRp(new RpRating(bean.getDirectorRP()));
			director.setRemark(bean.getDirectorRemark());
			model.setDirector(director);
		}
		return model;
	}
	
	public List<HospitalDepartmentDBOnlineBean> findHospitalDepartmentByHospital(BigDecimal hospital){
		List<HospitalDepartmentDBOnlineBean> listBean = new ArrayList<HospitalDepartmentDBOnlineBean>(0);
		try{
			List<HospitalDepartment> listModel = new HospitalDepartmentHelper().findHospitalDepartmentByHospital(hospital);
			if (listModel != null && !listModel.isEmpty()){
				for(HospitalDepartment hosDept : listModel){
					listBean.add(this.setHospitalDepartmentToHospitalDepartmentDBOnlineBean(hosDept));
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return listBean;
	}
	
	public HospitalDepartmentDBOnlineBean findHospitalDepartmentByHospitalAndDepartment(BigDecimal hospital, BigDecimal department){
		HospitalDepartmentDBOnlineBean bean = null;
		try{
			HospitalDepartment model = new HospitalDepartmentHelper().findHospitalDepartmentByHospitalAndDepartment(hospital, department);
			if (model != null){
				bean = this.setHospitalDepartmentToHospitalDepartmentDBOnlineBean(model);
				bean.setHospital(new HospitalDBOnlineControl().findHospitalByHospitalCode(model.getHospitalCode()));
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return bean;
	}
	
	public HospitalDepartmentDBOnlineBean saveHospitalDepartment(HospitalDepartmentDBOnlineBean bean) throws Exception{
		try{
			
			if (bean != null){
				HospitalDepartment model = this.setHospitalDepartmentDBOnlineBeanToHospitalDepartment(bean);
				model = new HospitalDepartmentHelper().save(null, model);
				if (model == null || model.getHospitalDepartmentCode() != null){
					throw new Exception("Can't save department of hospital.");
				}
				bean = this.setHospitalDepartmentToHospitalDepartmentDBOnlineBean(model);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return bean;
	}
}
